package com.neusoft.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.common.CommonReslut;
import com.neusoft.entity.RoleAuth;
import com.neusoft.service.RoleAuthService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * (RoleAuth)表控制层
 *
 * @author makejava
 * @since 2020-06-05 20:49:57
 */
@Controller
@RequestMapping("auth")
public class RoleAuthController {
    /**
     * 服务对象
     */
    @Resource
    private RoleAuthService roleAuthService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public RoleAuth selectOne(Integer id) {
        return this.roleAuthService.queryById(id);
    }

    //管理查询界面
    @RequestMapping("tolist")
    public String tolist() {
        return "/function/auth_table";
    }
    //admin页面的查询所有功能
    @PostMapping("queryAll")
    @ResponseBody
    public Object queryAll(@RequestParam("page") Integer page, @RequestParam("limit")Integer limit, @RequestBody RoleAuth roleAuth){
        CommonReslut<RoleAuth> result=new CommonReslut<>();
        IPage<RoleAuth> iPage =roleAuthService.queryAllByLimit(page,limit,roleAuth);
        result.setCode(0);
        System.out.println("iPage.getTotal():::"+iPage.getTotal());
        System.out.println("iPage.getRecords():::"+iPage.getRecords());
        result.setCount(iPage.getTotal());
        result.setData(iPage.getRecords());
        return result;
    }

    //管理添加界面
    @RequestMapping("toadd")
    public String toadd(Model model) {
        model.addAttribute("roleAuth",new RoleAuth());
        return "/function/auth_add";
    }

    //管理添加保存功能
    @PostMapping("save")
    @ResponseBody
    public Object save(@RequestBody RoleAuth roleAuth) {
        boolean result=false;

        if(roleAuth.getId()!=null){
            //编辑
            result =roleAuthService.update(roleAuth)>0;
        }else{
            //添加
            result=roleAuthService.insert(roleAuth).getId()!=null;
        }


        return result;
    }
    //管理更新功能
    @RequestMapping("toedit")
    public String toedit(int id,Model model) {

        System.out.println("id:::"+id);
        RoleAuth roleAuth=roleAuthService.queryById(id);
        model.addAttribute("roleAuth",roleAuth);

        return "function/auth_add";
    }

    //管理删除功能
    @PostMapping("delete")
    @ResponseBody
    public boolean delete(@RequestBody Integer[] ids) {

        if(ids==null ||ids.length==0)
            return false;
//        System.out.println(ids[0]);
//        return true;

        return roleAuthService.deleteById(Arrays.asList(ids));
    }

}